package com.cloudkinto.controller.doorpick;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.service.doorpick.vo.FinishProblemOrderReq;
import com.cloudkinto.service.doorpick.vo.OrderDoorPickAddReq;
import com.cloudkinto.service.doorpick.vo.OrderDoorPickPageRes;
import com.cloudkinto.service.doorpick.vo.OrderDoorUpdateReq;
import io.swagger.annotations.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.doorpick.OrderDoorPickService;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.Map;

import com.cloudkinto.base.BaseController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author hua
 * @since 2024-03-14
 */
@RestController
@Api(tags = "上门取件模块")
@RequestMapping("/api/OrderDoorPick")
public class OrderDoorPickController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(OrderDoorPickController.class);

    @Autowired
    private OrderDoorPickService service;

    @UserSessionRequired
    @PostMapping("")
    @ApiOperation(value = "新增", notes = "新增")
    public SingleResult add(@RequestBody @Validated OrderDoorPickAddReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("新增成功", service.add(req, userId, companyId));
    }

    @UserSessionRequired
    @PutMapping("")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated OrderDoorUpdateReq req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.update(req, userId, getCompanyId()));
    }

    @UserSessionRequired
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功", service.delete(id, userId, getCompanyId()));
    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit(getCurrentUserId(), getCompanyId());
    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "shopId", value = "电瓶id"),
            @ApiImplicitParam(name = "status", value = "状态"),
            @ApiImplicitParam(name = "beginTime", value = "开始时间"),
            @ApiImplicitParam(name = "endTime", value = "结束时间"),
            @ApiImplicitParam(name = "keyword", value = "上门单号或快递单号"),
            @ApiImplicitParam(name = "storageId", value = "仓库id")})
    @ApiResponses(@ApiResponse(code = 200, message = "", response = OrderDoorPickPageRes.class))
    public PageResult listPage(HttpServletRequest request){
        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");

        String beginTime = request.getParameter("beginTime");
        String endTime = request.getParameter("endTime");
        String shopId = request.getParameter("shopId");
        String status = request.getParameter("status");
        String storageId = request.getParameter("storageId");
        map.put("beginTime", beginTime);
        map.put("endTime", endTime);
        map.put("shopId", shopId);
        map.put("status", status);
        map.put("storageId", storageId);
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", size);

        Long companyId = getCompanyId();
        if (companyId != null) {
            map.put("companyId", companyId+"");
        }
        return service.getListPage(map);
    }

    @UserSessionRequired
    @GetMapping("/getOrderMsgByTrackNumber")
    @ApiOperation(value = "根据快递单号获取订单信息", notes = "根据快递单号获取订单信息")
    public SingleResult getOrderMsgByTrackNumber(HttpServletRequest request){
        Map<String, String> map = new HashMap<>();
        String trackNumber = request.getParameter("trackNumber");
        String beginTime = request.getParameter("beginTime");
        String endTime = request.getParameter("endTime");
        return SingleResult.success(service.getOrderMsgByTrackNumber(trackNumber, beginTime, endTime, getCompanyId()));
    }

    @UserSessionRequired
    @PutMapping("/cutPickOrder")
    @ApiOperation(value = "截单 已审核预约成功状态可操作", notes = "截单 已审核预约成功状态可操作")
    public SingleResult cutPickOrder(@RequestBody @Validated FinishProblemOrderReq req){
        service.cutPickOrder(req, getCurrentUserId());
        return SingleResult.success();
    }

}

